/*
 * @Author: zi.yang
 * @Date: 2020-06-18 00:40:05
 * @LastEditTime: 2021-10-11 14:13:28
 * @LastEditors: zi.yang
 * @Description: user router
 * @FilePath: \project-management\controllers\sysUser.js
 */

const express = require('express');
const router = express.Router();
const SysUser = require('../services/SysUser');
const GetIPAdress = require('../utils/getIPAdress');
const multer = require('multer');
var path = require('path');
const fs = require('fs');
const getIPAdress = new GetIPAdress();

const { UPLOAD_PATH } = require('../config/setting');
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, UPLOAD_PATH + '/avatar');
  },
  filename: function (req, file, cb) {
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
    var extname = path.extname(file.originalname);
    cb(null, uniqueSuffix + extname);
  },
});
const upload = multer({ storage }).single('avatar');

router.get('/findAll', function (req, res) {
  SysUser.findUserInfo((data) => {
    res.status(data.code).json(data);
  });
});

router.post('/login', function (req, res) {
  const ip = getIPAdress.getClientIp(req);
  SysUser.findUserForLogin(req.body, ip, (data) => {
    res.status(data.code).json(data);
  });
});

router.post('/refreshToken', function (req, res) {
  const { loginTime, userinfo } = req.body;
  SysUser.refreshToken(userinfo, loginTime, (data) => {
    res.status(data.code).json(data);
  });
});

router.get('/findInfoByUsername', function (req, res) {
  const { username } = req.query;
  SysUser.findUserByUsername(username, (data) => {
    res.status(data.code).json(data);
  });
});

router.get('/findInfoById', function (req, res) {
  const { id } = req.query;
  SysUser.findUserInfoById(id, (data) => {
    res.status(data.code).json(data);
  });
});

router.post('/addUser', function (req, res) {
  SysUser.addUser(req.body, (data) => {
    res.status(data.code).json(data);
  });
});

router.put('/updateUserById', function (req, res) {
  const { id, userinfo } = req.body;
  SysUser.updatedUserById(id, userinfo, (data) => {
    res.status(data.code).json(data);
  });
});

router.post('/uploadAvatar', function (req, res) {
  upload(req, res, function (err) {
    if (err) {
      res.status(500).json({ code: 500, msg: '上传失败', data: {} });
    } else {
      const apiPath = `${req.headers.origin}/api/users/avatar/${req.file.filename}`
      res.status(200).json({ code: 200, msg: '上传成功', data: {path:apiPath} });
    }
  });
});

router.get('/avatar/:filename', function (req, res) {
  fs.readFile(`${UPLOAD_PATH}/avatar/${req.params.filename}`, 'binary', function (err, file) {
    if (err) {
      res.status(500).json({ code: 500, msg: '未找到头像', data: {} });
    } else {
      res.writeHead(200,  {'Content-Type':'image/jpeg'})
      res.write(file, 'binary')
      res.end();
    } 
  });
});

router.delete('/deleteUserById', function (req, res) {
  SysUser.deleteUserById(req.query.id, (data) => {
    res.status(data.code).json(data);
  });
});

router.put('/resetPasswordById', function (req, res) {
  SysUser.resetPasswordById(req.body.id, (data) => {
    res.status(data.code).json(data);
  });
});

router.put('/changePasswordById', function (req, res) {
  SysUser.changePasswordById(req.body.id, req.body.password, (data) => {
    res.status(data.code).json(data);
  });
});
module.exports = router;
